{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "provenance": []
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    },
    "language_info": {
      "name": "python"
    }
  },
  "cells": [
    {
      "cell_type": "markdown",
      "source": [
        "# Parse Resumes with Tensorlake\n",
        "*Author: [Antaripa Saha](https://x.com/doesdatmaksense)*\n",
        "\n",
        "In this example you will learn, how to use extract structured data when parsing candidate resumes using Tensorlake DocumentAI. To learn more about Resume Parsing [check out the Tensorlake docs](https://docs.tensorlake.ai/use-cases/hr-and-onboarding/resume-parsing)"
      ],
      "metadata": {
        "id": "ilY98cmV6Nk9"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "Resume parsing is crucial in modern hiring workflows where recruiters deal with hundreds or thousands of resumes.\n",
        "Automating the extraction of key information (skills, experience, education) saves time and enables efficient candidate screening.\n",
        "It also powers recommendation engines, applicant tracking systems (ATS), and helps maintain structured databases of talent profiles."
      ],
      "metadata": {
        "id": "yp_CDcgFr_9W"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "# Why Traditional Resume Parsers Struggle\n",
        "\n",
        "Resume parsing sounds simple until you come across the real-world diversity of resumes. Here's a quick overview of why conventional tools often fail.\n",
        "\n",
        "**Format Variety**\n",
        "\n",
        "| Resume Type           | Format Description                        | Common Issues                                  |\n",
        "|------------------------|--------------------------------------------|------------------------------------------------|\n",
        "| Creative Designer      | PDF with columns, graphics, custom fonts   | Can't parse columns, OCR fails on fonts        |\n",
        "| Standard Corporate     | Clean Word doc                             | Date inconsistencies, skills in text blocks     |\n",
        "| Academic CV            | LaTeX, multi-page PDF                      | Complex tables, publication lists               |\n",
        "| International Resume   | Non-US formats                             | Date/address confusion, terminology mismatch    |\n",
        "| Scanned Resume         | Image-based PDFs                           | OCR errors, layout detection issues             |\n",
        "\n",
        "Even clean resumes can trip up parsers due to inconsistent phrasing or document structure.\n",
        "\n"
      ],
      "metadata": {
        "id": "ey7QrXLNvnek"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "## Step 0: Prerequisites\n",
        "\n",
        "1. Install the [Tensorlake SDK](https://pypi.org/project/tensorlake/)\n",
        "2. Import necessary packages\n",
        "3. Set your [Tensorlake API Key](https://docs.tensorlake.ai/platform/authentication)\n",
        "\n",
        "**Note:** Learn more with the [Tensorlake docs](https://docs.tensorlake.ai/)."
      ],
      "metadata": {
        "id": "njBcB3m749-h"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "!pip install -q --upgrade tensorlake"
      ],
      "metadata": {
        "collapsed": true,
        "id": "mligrnYVZhmk"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "4Un5rSYjZcIp"
      },
      "outputs": [],
      "source": [
        "# Import libraries\n",
        "from tensorlake.documentai import DocumentAI\n",
        "from tensorlake.documentai.models import (\n",
        "    ParsingOptions,\n",
        "    StructuredExtractionOptions,\n",
        "    ParseStatus,\n",
        "    ChunkingStrategy\n",
        ")\n",
        "import time\n",
        "import json"
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "%env TENSORLAKE_API_KEY=your_tensorlake_api_key"
      ],
      "metadata": {
        "id": "Z0CJaPdHZc0P"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "## Step 1: Specify Structured Data Extraction\n",
        "\n",
        "Create a simple JSON schema to specify what structured data you want extracted from the document"
      ],
      "metadata": {
        "id": "NmCSH2CstazZ"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "# JSON schema to extract relevant data from resumes in a structured format\n",
        "structured_schema = {\n",
        "  \"title\": \"ResumeInfo\",\n",
        "  \"type\": \"object\",\n",
        "  \"properties\": {\n",
        "    \"candidateName\":   { \"type\": \"string\" },\n",
        "    \"email\":           { \"type\": \"string\" },\n",
        "    \"phone\":           { \"type\": \"string\" },\n",
        "    \"address\":         { \"type\": \"string\" },\n",
        "\n",
        "    \"professionalSummary\": { \"type\": \"string\" },\n",
        "    \"skills\": {\n",
        "      \"type\": \"array\",\n",
        "      \"items\": { \"type\": \"string\" }\n",
        "    },\n",
        "    \"workExperience\": {\n",
        "      \"type\": \"array\",\n",
        "      \"items\": {\n",
        "        \"type\": \"object\",\n",
        "        \"properties\": {\n",
        "          \"jobTitle\":     { \"type\": \"string\" },\n",
        "          \"companyName\":  { \"type\": \"string\" },\n",
        "          \"location\":     { \"type\": \"string\" },\n",
        "          \"startDate\":    { \"type\": \"string\" },\n",
        "          \"endDate\":      { \"type\": \"string\" },\n",
        "          \"description\":  { \"type\": \"string\" }\n",
        "        }\n",
        "      }\n",
        "    },\n",
        "    \"education\": {\n",
        "      \"type\": \"array\",\n",
        "      \"items\": {\n",
        "        \"type\": \"object\",\n",
        "        \"properties\": {\n",
        "          \"degree\":        { \"type\": \"string\" },\n",
        "          \"fieldOfStudy\":  { \"type\": \"string\" },\n",
        "          \"institution\":   { \"type\": \"string\" },\n",
        "          \"location\":      { \"type\": \"string\" },\n",
        "          \"graduationDate\":{ \"type\": \"string\" }\n",
        "        }\n",
        "      }\n",
        "    }\n",
        "  }\n",
        "}"
      ],
      "metadata": {
        "id": "T8sACwLOZc47"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "## Step 2: Parse the Document\n",
        "To use the Tensorlake Python SDK, you need to:\n",
        "\n",
        "1. Create a Tensorlake Client\n",
        "2. Specify a file, in this a candidate's resume file\n",
        "3. Specify Parsing Options, including page numbers and a chunking strategy for parsing\n",
        "4. Specify Structured Extraction Options, which need to include a schema name and schema at minimum.\n",
        "5. Initiate the parsing job and wait until it compeltes successfully"
      ],
      "metadata": {
        "id": "Y3LyACJEtlSS"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "# Create a Tensorlake Client, this will reference the `TENSORLAKE_API_KEY` environment variable you set above\n",
        "doc_ai = DocumentAI()\n",
        "\n",
        "# Reference to a resume that you want to parse\n",
        "file_path = 'https://pub-226479de18b2493f96b64c6674705dd8.r2.dev/jakes-resume.pdf'\n",
        "\n",
        "# Configure parsing with structured schema\n",
        "parsing_options = ParsingOptions(\n",
        "    chunking_strategy=ChunkingStrategy.PAGE\n",
        ")\n",
        "\n",
        "structured_extraction_options = StructuredExtractionOptions(\n",
        "    schema_name=\"Candidate Resume\",\n",
        "    json_schema=structured_schema  # schema for structured extraction\n",
        ")\n",
        "\n",
        "# Parse the document with the specified extraction options for structured data\n",
        "parse_id = doc_ai.parse(file_path, parsing_options=parsing_options, structured_extraction_options=[structured_extraction_options])\n",
        "\n",
        "print(f\"Parse job submitted with ID: {parse_id}\")\n",
        "\n",
        "# Wait for completion\n",
        "result = doc_ai.wait_for_completion(parse_id)"
      ],
      "metadata": {
        "id": "pRW6ZLpz2AxN",
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "outputId": "f7c1ae48-ecae-4266-98ed-8af077ae8094"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "Parse job submitted with ID: parse_mLpNjGGr8NHFkTzfrGgfW\n",
            "waiting 5 s…\n",
            "parse status: processing\n",
            "waiting 5 s…\n",
            "parse status: processing\n",
            "waiting 5 s…\n",
            "parse status: processing\n",
            "waiting 5 s…\n",
            "parse status: processing\n",
            "waiting 5 s…\n",
            "parse status: processing\n",
            "waiting 5 s…\n",
            "parse status: successful\n"
          ]
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "# Understanding Tensorlake Parsing Output\n",
        "\n",
        "In one single DocumentAI API call, Tensorlake returns both the full markdown content of the document and the structured data in JSON format.\n"
      ],
      "metadata": {
        "id": "bxPKR73NgOMc"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "## Review the Structured Data Output"
      ],
      "metadata": {
        "id": "uvb_KdshU-R5"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "# Print the structured data output\n",
        "print(json.dumps(result.structured_data[0].data, indent=2))"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "QWbNwXYp2jar",
        "outputId": "e8656dbc-9d06-4a97-a048-9fc7ddf844a9"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "{\n",
            "  \"address\": null,\n",
            "  \"candidateName\": \"Jake Ryan\",\n",
            "  \"education\": [\n",
            "    {\n",
            "      \"degree\": \"Bachelor of Arts in Computer Science, Minor in Business\",\n",
            "      \"fieldOfStudy\": \"Computer Science\",\n",
            "      \"graduationDate\": \"May 2021\",\n",
            "      \"institution\": \"Southwestern University\",\n",
            "      \"location\": \"Georgetown, TX\"\n",
            "    },\n",
            "    {\n",
            "      \"degree\": \"Associate's in Liberal Arts\",\n",
            "      \"fieldOfStudy\": \"Liberal Arts\",\n",
            "      \"graduationDate\": \"May 2018\",\n",
            "      \"institution\": \"Blinn College\",\n",
            "      \"location\": \"Bryan, TX\"\n",
            "    }\n",
            "  ],\n",
            "  \"email\": \"jake@su.edu\",\n",
            "  \"phone\": \"123-456-7890\",\n",
            "  \"professionalSummary\": null,\n",
            "  \"skills\": [\n",
            "    \"Java\",\n",
            "    \"Python\",\n",
            "    \"C/C++\",\n",
            "    \"SQL (Postgres)\",\n",
            "    \"JavaScript\",\n",
            "    \"HTML/CSS\",\n",
            "    \"R\",\n",
            "    \"React\",\n",
            "    \"Node.js\",\n",
            "    \"Flask\",\n",
            "    \"JUnit\",\n",
            "    \"WordPress\",\n",
            "    \"Material-UI\",\n",
            "    \"FastAPI\",\n",
            "    \"Git\",\n",
            "    \"Docker\",\n",
            "    \"TravisCI\",\n",
            "    \"Google Cloud Platform\",\n",
            "    \"VS Code\",\n",
            "    \"Visual Studio\",\n",
            "    \"PyCharm\",\n",
            "    \"IntelliJ\",\n",
            "    \"Eclipse\",\n",
            "    \"pandas\",\n",
            "    \"NumPy\",\n",
            "    \"Matplotlib\"\n",
            "  ],\n",
            "  \"workExperience\": [\n",
            "    {\n",
            "      \"companyName\": \"Texas A&M University\",\n",
            "      \"description\": \"\\u00b7 Developed a REST API using FastAPI and PostgreSQL to store data from learning management systems\\n\\u00b7 Developed a full-stack web application using Flask, React, PostgreSQL and Docker to analyze GitHub data\\n\\u00b7 Explored ways to visualize GitHub collaboration in a classroom setting\",\n",
            "      \"endDate\": null,\n",
            "      \"jobTitle\": \"Undergraduate Research Assistant\",\n",
            "      \"location\": \"College Station, TX\",\n",
            "      \"startDate\": \"June 2020\"\n",
            "    },\n",
            "    {\n",
            "      \"companyName\": \"Southwestern University\",\n",
            "      \"description\": \"\\u00b7 Communicate with managers to set up campus computers used on campus\\n\\u00b7 Assess and troubleshoot computer problems brought by students, faculty and staff\\n\\u00b7 Maintain upkeep of computers, classroom equipment, and 200 printers across campus\",\n",
            "      \"endDate\": null,\n",
            "      \"jobTitle\": \"Information Technology Support Specialist\",\n",
            "      \"location\": \"Georgetown, TX\",\n",
            "      \"startDate\": \"Sep. 2018\"\n",
            "    },\n",
            "    {\n",
            "      \"companyName\": \"Southwestern University\",\n",
            "      \"description\": \"\\u00b7 Explored methods to generate video game dungeons based off of The Legend of Zelda\\n\\u00b7 Developed a game in Java to test the generated dungeons\\n\\u00b7 Contributed 50K+ lines of code to an established codebase via Git\\n\\u00b7 Conducted a human subject study to determine which video game dungeon generation technique is enjoyable\\n\\u00b7 Wrote an 8-page paper and gave multiple presentations on-campus\\n\\u00b7 Presented virtually to the World Conference on Computational Intelligence\",\n",
            "      \"endDate\": \"July 2019\",\n",
            "      \"jobTitle\": \"Artificial Intelligence Research Assistant\",\n",
            "      \"location\": \"Georgetown, TX\",\n",
            "      \"startDate\": \"May 2019\"\n",
            "    }\n",
            "  ]\n",
            "}\n"
          ]
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "## Review the Markdown Chunks Output"
      ],
      "metadata": {
        "id": "1RAKCh3ftMAq"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "# Get the markdown from extracted data\n",
        "for index, chunk in enumerate(result.chunks):\n",
        "    print(f\"Chunk {index}:\")\n",
        "    print(chunk.content)"
      ],
      "metadata": {
        "id": "ikeITyB8eBVv",
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "outputId": "de281a06-4b05-4660-b567-9a47981e64c7"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "Chunk 0:\n",
            "Jake Ryan\n",
            "123-456-7890 | jake@su.edu | linkedin.com/in/jake | github.com/jake\n",
            "\n",
            "## EDUCATION\n",
            "\n",
            "\n",
            "<table>\n",
            "<tr>\n",
            "<th>Southwestern University</th>\n",
            "<th>Georgetown, TX</th>\n",
            "</tr>\n",
            "<tr>\n",
            "<td>Bachelor of Arts in Computer Science, Minor in Business</td>\n",
            "<td>Aug. 2018 - May 2021</td>\n",
            "</tr>\n",
            "<tr>\n",
            "<td>Blinn College</td>\n",
            "<td>Bryan, TX</td>\n",
            "</tr>\n",
            "<tr>\n",
            "<td>Associate's in Liberal Arts</td>\n",
            "<td>Aug. 2014 - May 2018</td>\n",
            "</tr>\n",
            "</table>\n",
            "\n",
            "\n",
            "## EXPERIENCE\n",
            "\n",
            "\n",
            "### Undergraduate Research Assistant\n",
            "\n",
            "Texas A&M University\n",
            "June 2020 - Present College Station, TX\n",
            "· Developed a REST API using FastAPI and PostgreSQL to store data from learning management systems\n",
            "· Developed a full-stack web application using Flask, React, PostgreSQL and Docker to analyze GitHub data\n",
            "· Explored ways to visualize GitHub collaboration in a classroom setting\n",
            "\n",
            "### Information Technology Support Specialist Southwestern University\n",
            "\n",
            "Sep. 2018 - Present Georgetown, TX\n",
            "· Communicate with managers to set up campus computers used on campus\n",
            "· Assess and troubleshoot computer problems brought by students, faculty and staff\n",
            "· Maintain upkeep of computers, classroom equipment, and 200 printers across campus\n",
            "Artificial Intelligence Research Assistant Southwestern University May 2019 - July 2019 Georgetown, TX\n",
            "· Explored methods to generate video game dungeons based off of The Legend of Zelda\n",
            "· Developed a game in Java to test the generated dungeons\n",
            "· Contributed 50K+ lines of code to an established codebase via Git\n",
            "· Conducted a human subject study to determine which video game dungeon generation technique is enjoyable\n",
            "· Wrote an 8-page paper and gave multiple presentations on-campus\n",
            "· Presented virtually to the World Conference on Computational Intelligence\n",
            "\n",
            "## PROJECTS\n",
            "\n",
            "Gitlytics | Python, Flask, React, PostgreSQL, Docker June 2020 - Present\n",
            "· Developed a full-stack web application using with Flask serving a REST API with React as the frontend\n",
            "· Implemented GitHub OAuth to get data from user's repositories\n",
            "· Visualized GitHub data to show collaboration\n",
            "· Used Celery and Redis for asynchronous tasks\n",
            "Simple Paintball | Spigot API, Java, Maven, TravisCI, Git\n",
            "May 2018 - May 2020\n",
            "· Developed a Minecraft server plugin to entertain kids during free time for a previous job\n",
            "· Published plugin to websites gaining 2K+ downloads and an average 4.5/5-star review\n",
            "· Implemented continuous delivery using TravisCI to build the plugin upon new a release\n",
            "· Collaborated with Minecraft server administrators to suggest features and get feedback about the plugin\n",
            "\n",
            "### TECHNICAL SKILLS\n",
            "\n",
            "Languages: Java, Python, C/C++, SQL (Postgres), JavaScript, HTML/CSS, R Frameworks: React, Node.js, Flask, JUnit, WordPress, Material-UI, FastAPI Developer Tools: Git, Docker, TravisCI, Google Cloud Platform, VS Code, Visual Studio, PyCharm, IntelliJ, Eclipse Libraries: pandas, NumPy, Matplotlib\n",
            "\n"
          ]
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "# Next Steps\n",
        "\n",
        "Now that you have the basics down, check out one of these other resources to dive deeper into document parsing with Tensorlake:\n",
        "- [Python SDK and API Docs](https://docs.tensorlake.ai/)\n",
        "- [Blog](https://tensorlake.ai/blog)\n",
        "- [YouTube Channel](https://tensorlake.ai/blog)\n",
        "- [Community Slack](https://tensorlakecloud.slack.com/)"
      ],
      "metadata": {
        "id": "1APMsU_U6bfq"
      }
    }
  ]
}